<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>文章管理</title>
    <jsp:include page="/resources/inc/head.jsp" flush="true"/>
    <link href="${basePath}/resources/zheng-admin/plugins/bootstrap-fileinput-master/css/fileinput.css" rel="stylesheet"/>
    <link href="${basePath}/resources/zheng-admin/plugins/bootstrap-fileinput-master/css/fileinput-rtl.css" rel="stylesheet"/>
</head>
<body>
<div class="container-fuid" style="height:100%;">
    <div class="form-group" style="height:100%;" >
        <div class="col-sm-3" style="height:100%;">
            <div class="panel panel-default" style="height:100%;">
                <div class="panel-heading">目录树</div>
                <div class="panel-body">
                    <ul id="ztree" class="ztree"></ul>
                </div>
            </div>
        </div>
        <div class="col-sm-9">
            <%--<div id="main">--%>
                <div id="toolbar">
                    <shiro:hasPermission name="wechatmp:article:create"><a class="waves-effect waves-button" href="javascript:;" onclick="createAction()"><i class="zmdi zmdi-plus"></i> 新增</a></shiro:hasPermission>
                    <shiro:hasPermission name="wechatmp:article:update"><a class="waves-effect waves-button" href="javascript:;" onclick="updateAction()"><i class="zmdi zmdi-edit"></i> 编辑</a></shiro:hasPermission>
                    <shiro:hasPermission name="wechatmp:article:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="deleteAction()"><i class="zmdi zmdi-close"></i> 删除</a></shiro:hasPermission>
                    <shiro:hasPermission name="wechatmp:article:imageManage"><a class="waves-effect waves-button" href="javascript:;" onclick="imagesAction()"><i class="glyphicon glyphicon-equalizer"></i> 管理图片</a></shiro:hasPermission>
                </div>
                <table id="table"></table>
            <%--</div>--%>
        </div>
    </div>
</div>

<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
<script src="${basePath}/resources/zheng-admin/plugins/bootstrap-fileinput-master/js/fileinput.min.js"></script>
<script src="${basePath}/resources/zheng-admin/plugins/bootstrap-fileinput-master/js/locales/zh.js"></script>
<script>
    var categoryId = null;
    var setting = {
//        check: {
//            enable: true,
//            // 勾选关联父，取消关联子
//            chkboxType: { "Y" : "p", "N" : "s" }
//        },
        async: {
            enable: true,
            autoParam: ["id"],
            url: '${basePath}/manage/article/category'
        },
        data: {
            simpleData: {
                enable: false
            }
        },
        callback: {
            onClick : function(event, treeId, treeNode, clickFlag) {
                // 判断是否父节点
                if(treeNode){
                    categoryId = treeNode.id;
                    $table.bootstrapTable('refresh');
                }
//                if(!treeNode.isParent){
//                    alert("treeId自动编号：" + treeNode.tId + ", 节点id是：" + treeNode.id + ", 节点文本是：" + treeNode.name);
//                }
            }
        }
    };


    var $table = $('#table');
    $(function() {

        $.fn.zTree.init($('#ztree'), setting);

        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/manage/article/list',
            queryParams :queryParams,
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            detailView: true,
            detailFormatter: 'detailFormatter',
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'id',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'jwid', title: '公号ID'},
                {field: 'title', title: '标题'},
                {field: 'content', title: '内容'},
                {field: 'description', title: '描述'},
                {field: 'status', title: '状态',formatter: function (value, row, index) {
                    return getDictText("MP_AUDIT_STATUS",value);
                }},
                {field: 'orders', title: '排序号'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: true}
            ]
        });

//        $('#treePanel').attr("style",'height:'+getHeight()+'px;');
    });
    window.actionEvents = {
        <shiro:hasPermission name="wechatmp:article:update">
        'click .update': function(e, value, row, index) {
            updateAction([row]);
        },
        </shiro:hasPermission>
        <shiro:hasPermission name="wechatmp:article:delete">
        'click .delete': function(e, value, row, index) {
            deleteAction([row]);
        }
        </shiro:hasPermission>
    };
    //得到查询的参数
    function queryParams(params) {
        var temp = {};
        for (var k in params){
            temp[k] = params[k];
        }
        if(categoryId){
            temp.categoryId = categoryId;
        }
        return temp;
//        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
//            // limit: params.limit,   //页面大小
//            offset: params.offset,  //页码
//            pageSize: this.pageSize,
//            pageNumber: this.pageNumber,
////            projectId: $("#projectId").val(),
////            requirement_Id: $("#requirement_Id").val(),
//            length: 6
//        };
//        return temp;
    }
    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            <shiro:hasPermission name="wechatmp:article:update">
            '<a class="update" href="javascript:;" data-toggle="tooltip" title="Edit"><i class="glyphicon glyphicon-edit"></i></a>　',
            </shiro:hasPermission>
            <shiro:hasPermission name="wechatmp:article:delete">
            '<a class="delete" href="javascript:;" data-toggle="tooltip" title="Remove"><i class="glyphicon glyphicon-remove"></i></a>'
            </shiro:hasPermission>
        ].join('');
    }

    var iamgeManageDialog;
    function imagesAction() {
        var rows = $table.bootstrapTable('getSelections');
        if(rows.length>1 || rows.length==0){
            infoDialog("请选择一个文章。");
        }
        iamgeManageDialog = openDialog('url:${basePath}/manage/article/iamgeManage/'+rows[0].id,'图片管理');
    }

    // 新增
    var createDialog;
    function createAction() {
        if(!checkCategoryId()){
            return;
        }
        createDialog = openDialog('url:${basePath}/manage/article/create/'+categoryId,'新增')
    }
    // 编辑
    var updateDialog;
    function updateAction(rows) {

        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length != 1) {
            infoDialog('请选择一条记录！');
        } else {
            updateDialog = openDialog('url:${basePath}/manage/article/update/'+rows[0].id,'编辑');
        }
    }
    // 删除
    var deleteDialog;
    function deleteAction(rows) {
        if(!rows)
            rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            infoDialog( '请至少选择一条记录！');
        } else {
            deleteDialog = confirmDialog('确认删除该数据吗？',function(){
                var ids = new Array();
                for (var i in rows) {
                    ids.push(rows[i].id);
                }
                $.ajax({dataType: 'json',
                    type: 'get',
                    url: '${basePath}/manage/article/delete/' + ids.join("-"),
                    success: function(result) {
                        if (result.code != 1) {
                            if (result.data instanceof Array) {
                                $.each(result.data, function(index, value) {
                                    infoDialog(value.errorMsg);
                                });
                            } else {
                                infoDialog(result.data);
                            }
                        } else {
                            deleteDialog.close();
                            $table.bootstrapTable('refresh');
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        infoDialog(textStatus);
                    }
                });
            });
        }
    }

    function checkCategoryId(){
        if(!categoryId){
            infoDialog("请先选择目录。");
            return false;
        }
        return true;
    }

</script>
</body>
</html>